using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Utilities;

namespace GPX
{
    public partial class EditAxesDialog : FormEx
    {
        private System.Resources.ResourceManager ResourceManager
        {
            get { return GPXAppResourceManager.ResourceManager; }
        }

        private EnumDescConverter<DrawCategory> _DrawCategoryConverter;
        private EnumDescConverter<GMap.NET.MapType> _MaptypeConverter;


        public EditAxesDialog()
        {
            InitializeComponent();
            _DrawCategoryConverter = new EnumDescConverter<DrawCategory>(this.ResourceManager);
            _MaptypeConverter = new EnumDescConverter<GMap.NET.MapType>();
        }

        public event EventHandler RedrawRequested;

        #region Variables and Properties
        private bool _UseEqualScale;
        internal bool UseEqualScale
        {
            get { return _UseEqualScale; }
            set { _UseEqualScale = value; }
        }

        private Single _Zoom;
        internal Single Zoom
        {
            get { return _Zoom; }
            set { _Zoom = value; }
        }

        private bool _UseMap;
        internal bool UseMap
        {
            get { return _UseMap; }
            set { _UseMap = value; }
        }

        private DrawCategory _XCategory;
        internal DrawCategory XCategory
        {
            get { return _XCategory; }
            set { _XCategory = value; }
        }

        private DrawCategory _YCategory;
        internal DrawCategory YCategory
        {
            get { return _YCategory; }
            set { _YCategory = value; }
        }

        private bool _ShowMap;
        internal bool ShowMap
        {
            get { return _ShowMap; }
            set { _ShowMap = value; }
        }

        private GMap.NET.MapType _MapType;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal GMap.NET.MapType MapType
        {
            get { return _MapType; }
            set { _MapType = value; }
        }

        private decimal _LastPointMaximum;
        internal decimal LastPointMaximum
        {
            get { return _LastPointMaximum; }
            set { _LastPointMaximum = value; }
        }

        private bool _ShowAllPointsOfSegment;
        internal bool ShowAllPointsOfSegment
        {
            get { return _ShowAllPointsOfSegment; }
            set { _ShowAllPointsOfSegment = value; }
        }

        private int _IndexOfFirstPointShown;
        internal int IndexOfFirstPointShown
        {
            get { return _IndexOfFirstPointShown; }
            set { _IndexOfFirstPointShown = value; }
        }

        private int _IndexOfLastPointShown;
        internal int IndexOfLastPointShown
        {
            get { return _IndexOfLastPointShown; }
            set { _IndexOfLastPointShown = value; }
        }

        private DateTime _CacheDate;
        internal DateTime CacheDate
        {
            get { return _CacheDate; }
            set { _CacheDate = value; }
        }
        #endregion Variables and Properties

        private void EditAxesDialog_Load(object sender, EventArgs e)
        {
            Localizer.LocalizeControl(this, ResourceManager);

            string[] drawCategories = _DrawCategoryConverter.GetDescriptions().ToArray();
            cmbXAxis.Items.Clear();
            cmbXAxis.Items.AddRange(drawCategories);
            cmbYAxis.Items.Clear();
            cmbYAxis.Items.AddRange(drawCategories);

            //TODO: not yet localized
            string[] mapTypes = _MaptypeConverter.GetDescriptions().ToArray();
            cmbMapType.Items.Clear();
            cmbMapType.Items.AddRange(mapTypes);

            chkScale.Checked = _UseEqualScale;
            nudZoom.Value = Convert.ToDecimal(_Zoom);
            chkMap.Checked = _UseMap;
            cmbXAxis.SelectedItem = _DrawCategoryConverter.GetEnumDescription(_XCategory);
            cmbYAxis.SelectedItem = _DrawCategoryConverter.GetEnumDescription(_YCategory);
            chkMap.Checked = _ShowMap;
            cmbMapType.SelectedItem = _MaptypeConverter.GetEnumDescription(_MapType);

            nudLastPoint.Maximum = nudFirstPoint.Maximum = _LastPointMaximum;
            nudFirstPoint.Minimum = nudLastPoint.Minimum = 1;
            chkSelPoints.Checked = !_ShowAllPointsOfSegment;
            nudFirstPoint.Value = _IndexOfFirstPointShown;
            nudLastPoint.Value = _IndexOfLastPointShown;
            dtCache.Value = _CacheDate;

            this.TopMost = true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                _UseEqualScale = chkScale.Checked;
                _Zoom = Convert.ToSingle(nudZoom.Value);
                _UseMap = chkMap.Checked;

                //get x category
                string descX = cmbXAxis.Text;
                _XCategory = _DrawCategoryConverter.GetEnumValueT(descX);
                //get y category
                string descY = cmbYAxis.Text;
                _YCategory = _DrawCategoryConverter.GetEnumValueT(descY);

                _ShowMap = chkMap.Checked;
                string mapType = cmbMapType.Text;
                _MapType = _MaptypeConverter.GetEnumValueT(mapType);

                _ShowAllPointsOfSegment = !chkSelPoints.Checked;
                _IndexOfLastPointShown = Convert.ToInt32(nudLastPoint.Value);
                _IndexOfFirstPointShown = Convert.ToInt32(nudFirstPoint.Value);

                _CacheDate = dtCache.Value.Date;

                //notify parent that changes are requested
                if (this.RedrawRequested != null)
                    this.RedrawRequested(this, null);

                if (!chkKeepOnTop.Checked)
                {
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void chkMap_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                chkScale.Enabled =
                    cmbXAxis.Enabled = cmbYAxis.Enabled =
                    !chkMap.Checked;
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }

        private void chkSelPoints_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                nudLastPoint.Enabled = nudFirstPoint.Enabled = chkSelPoints.Checked;
            }
            catch (Exception ex)
            {
                Utils.HandleException(ex);
            }
        }
    }
}